<?php

namespace app\home\controller;

use app\common\controller\Home;
use PHPMailer\PHPMailer\PHPMailer;

class User extends Home
{
    public function __construct()
    {
        parent::__construct();

        $this->UserModel = model('User.User');
    }

    public function index()
    {
        $userid = $this->request->param('userid',0);

        if($userid)
        {
            $User = $this->UserModel->find($userid);

            if(!$User)
            {
                $this->error('该用户不存在');
            }

            return $this->fetch('user',[
                'User' => $User
            ]);
        }

        return $this->fetch();
    }

    // 基本资料
    public function profile()
    {
        $navTitle = '基本资料';

        if($this->request->isPost())
        {
            $action = $this->request->param('action','');

            if($action == 'profile')
            {
                $params = $this->request->param();

                // 封装数据
                $data = [
                    'id' => $this->LoginUser['id'],
                    'nickname' => $params['nickname'],
                    'sex' => $params['sex'],
                    'province' => $params['province'],
                    'city' => $params['city'],
                    'district' => $params['district'],
                    'content' => $params['content']
                ];

                // isUpdate == true ?  save = 更新数据  : 插入
                $result = $this->UserModel->isUpdate(true)->save($data);

                if($result === FALSE)
                {
                    $this->error('更新基本资料失败');
                }else{
                    $this->success('更新基本资料成功',url('home/user/index'));
                }
            }

            if($action == 'avatar')
            {
                $avatar = upload('avatar');

                if($avatar['code'] === 0)
                {
                    $this->error($avatar['msg']);
                }

                $data = [
                    'id' => $this->LoginUser['id'],
                    'avatar' => $avatar['data']
                ];

                $result = $this->UserModel->isUpdate(true)->save($data);

                if($result === FALSE)
                {
                    @is_file('.' . $data['avatar']) && unlink('.' . $data['avatar']);
                    $this->error('更新头像失败');
                }else{
                    @is_file('.' . $this->LoginUser['avatar']) && unlink('.' . $this->LoginUser['avatar']);
                    $this->success('更新头像成功');
                }
            }
        }


        // 查询地区的省份
        $provinces = model('Region')->where(['grade' => 1])->select();
        
        // 查询市
        if($this->LoginUser['province'])
        {
            $citys = model('Region')->where(['parentid' => $this->LoginUser['province']])->select();
        }else{
            $citys = [];
        }

        // 查询区
        if($this->LoginUser['city'])
        {
            $districts = model('Region')->where(['parentid' => $this->LoginUser['city']])->select();
        }else{
            $districts = [];
        }

        return $this->fetch('',[
            'navTitle' => $navTitle,
            'provinces' => $provinces,
            'citys' => $citys,
            'districts' => $districts
        ]);
    }

    // 查询地区
    public function area()
    {
        if($this->request->isAjax())
        {
            $code = $this->request->param('code','');

            $list = model('Region')->where(['parentid' => $code])->select();

            if(!$list)
            {
                $this->error('选择地区不存在');
            }

            $this->success('查询成功',null,$list);
        }
    }

    public function record()
    {
        $navTitle = '积分消费记录';

        return $this->fetch('',[
            'navTitle' => $navTitle
        ]);
    }

    // 发送邮件
    public function email()
    {
        if($this->request->isAjax())
        {
            $email = $this->request->param('email','');

            // 返回json格式数据
            // return json($email);

            /* 
                判断该邮箱是否已注册了

                生成随机字符串

                设置session

                再随机字符串发送该邮箱

                手动输入字符串

                在注册功能 获取session那个字符串以及接收表单字符串 => 作判断
            */

            $User = $this->UserModel->where(['email' => $email])->find();

            if(!$User)
            {
                $this->error('该邮箱不存在');
            }

            $Email = md5($User['email'].$User['salt']);

            $content = "<a href='http://www.k2215.com/home/index/auth?email=$Email'>点击认证</a>";

            $mail = new PHPMailer(true);

            // 开启SMTP
            $mail->isSMTP(true);

            // SMTP服务器 qq => smtp.qq.com
            $mail->Host = "smtp.163.com";

            // 开启认证
            $mail->SMTPAuth = true;

            // 发送内容支持HTML
            $mail->IsHTML(true);

            // 账号
            $mail->Username = 'h1179011410@163.com';

            // 密码 => 不是邮箱密码，开启SMTP时的授权码
            $mail->Password = 'JYCBYAJWJWDWBDUY';

            // 端口 服务器需要465端口
            $mail->Port = 465;

            // 协议
            $mail->SMTPSecure = 'ssl';

            // 发件邮箱
            $mail->From = 'h1179011410@163.com';

            // 发件人名字
            $mail->FromName = "ASK论坛";

            // 接收回复的邮箱
            $mail->addReplyTo('h1179011410@163.com',"ASK论坛");

            // 收件人
            $mail->addAddress($User['email'],$User['nickname']);

            // 邮箱主题
            $mail->Subject = '账号认证';

            //设置多少个字符换行 80字符
            $mail->WordWrap = 80;

            // 发送HTML内容
            $mail->msgHTML($content);

            $result = $mail->send();

            if($result === FALSE)
            {
                $this->error('发送邮件失败');
            }else{
                $this->success('发送邮件成功');
            }

        }
    }

}
